package club.kingyin.kyblog.controller.web;

import club.kingyin.kyblog.core.IP;
import club.kingyin.kyblog.core.PassToken;
import club.kingyin.kyblog.core.business.dao.Comment;
import club.kingyin.kyblog.core.business.dao.Doc;
import club.kingyin.kyblog.core.business.dto.ResultUtil;
import club.kingyin.kyblog.core.business.service.DocService;
import club.kingyin.kyblog.core.business.vo.PageVo;
import club.kingyin.kyblog.core.business.vo.web.SearchVo;
import club.kingyin.kyblog.core.extend.IPSetPostProcessor;
import club.kingyin.kyblog.core.extend.LogLimitConsole;
import club.kingyin.kyblog.core.utils.cache.Cache;
import club.kingyin.kyblog.core.utils.log.LogConsole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * @author : kingyins
 * @version : 1.0
 * @program : ky-blog
 * @description : 在此处添加类描述
 * @create : 2021年08月24日 10:21
 **/
@IP
@RestController
@RequestMapping("/blog")
public class WebDocController {

    @Autowired
    private DocService docService;

    // 文章缓存一天
    @PassToken
    @Cache(module = "文章", operator = "九命可", sources = IPSetPostProcessor.class)
    @GetMapping("/article")
    public ResultUtil getArticle(@RequestParam("id") int id) {
        Doc doc = docService.getDocById(id);
        if (doc == null) {
            return ResultUtil.fail("没有这篇文章");
        }
        return ResultUtil.success(doc);
    }

    @PassToken
    @Cache(module = "文章列表", operator = "九命可", sources = IPSetPostProcessor.class)
    @PostMapping("/post/list")
    public ResultUtil searchList(@RequestBody SearchVo searchVo) {
        boolean isType = chickSearchVo(searchVo);
        PageVo pageVo = docService.webSearchList(searchVo.getValue(), searchVo.getPage(), 10, isType);
        if (pageVo != null) {
            return ResultUtil.success(pageVo);
        }
        return ResultUtil.fail("加载失败");
    }

    private boolean chickSearchVo(SearchVo searchVo) {
        if ("".equals(searchVo.getKey()) || "".equals(searchVo.getValue()))
            searchVo.setValue(null);
        if ("words".equals(searchVo.getKey())) {
            return false;
        } else {
            return true;
        }
    }

    @IP
    @PassToken
    @LogLimitConsole(module = "提交评论", operator = "九命可", paramsType = Comment.class, params = "docId")
    @PostMapping("/submit/comment")
    public ResultUtil submitComment(@RequestBody Comment comment) {
        return docService.createComment(comment) ? ResultUtil.success("您的评论会在审核通过后显示哦～") : ResultUtil.fail("可恶，没有评论成功啊！");
    }

    @PassToken
    @Cache(module = "加载评论", operator = "九命可", sources = IPSetPostProcessor.class)
    @GetMapping("/comment")
    public ResultUtil getComments(@RequestParam("id") int id) {
        ArrayList<Comment> comments = docService.getCommentsByDocId(id);
        if (comments == null) {
            return ResultUtil.fail("暂无评论");
        }
        return ResultUtil.success(docService.getCommentsByDocId(id));
    }
}
